//
//  CategoryViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/21.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "CategoryViewController.h"

#import "CategoryDetailViewController.h"//商品分类详情页Controller

#import "CategoriesCollectionViewCell.h"//collectionViewCell


#define TableViewWidth 100
#define TableViewCellHeight 70

@interface CategoryViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

@implementation CategoryViewController
{
    UITableView *_categoryTableView;//左边分类TableView
    NSArray *_categoryTableArray;//TableView数据
    
    UICollectionView *_categoryCollectionView;//右边分类详情CollectionView
    NSArray *_categoryCollectionArray;//CollectionView数据
    
}
 static NSString *const TableViewCellID = @"TableViewCell";
 static NSString *const CategoriesCollectionViewCellID = @"CategoriesCollectionViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"分类";
    UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
    back.title = @"返回";
    self.navigationItem.backBarButtonItem = back;
    
    //创建数据源
    [self createDataArray];
    //创建TableView
    [self createTableView];
    //创建CollectionView
    [self createCollectionView];
    
    //默认选中第一个cell
    NSIndexPath *first = [NSIndexPath indexPathForRow:0 inSection:0];
    [_categoryTableView selectRowAtIndexPath:first animated:NO scrollPosition:UITableViewScrollPositionTop];
}
#pragma mark === 创建TableView ===
- (void)createTableView{
    _categoryTableView= [[UITableView alloc]initWithFrame:CGRectMake(0, 108, TableViewWidth, HEIGHT - 64 - 94) style:UITableViewStylePlain];
    _categoryTableView.delegate = self;
    _categoryTableView.dataSource = self;
    _categoryTableView.separatorColor = [UIColor clearColor];
    _categoryTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
    [self.view addSubview:_categoryTableView];
    
    [_categoryTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TableViewCellID];
}
#pragma mark === 初始化数据源 ===
- (void)createDataArray{
    _categoryTableArray = @[@"海外购", @"数码家电", @"内衣", @"美妆配饰", @"温州美食", @"母婴", @"男装", @"杭州美食", @"文娱运动", @"西安美食", @"美妆配饰", @"美妆配饰"];
    _categoryCollectionArray = @[@"全部", @"海外购", @"彩妆香氛", @"全部", @"数码家电", @"彩妆香氛", @"全部", @"内衣", @"彩妆香氛", @"全部", @"美妆配饰", @"彩妆香氛", @"全部", @"温州美食", @"彩妆香氛", @"全部", @"母婴", @"彩妆香氛", @"全部", @"男装", @"彩妆香氛", @"全部", @"杭州美食", @"彩妆香氛", @"全部", @"文娱运动", @"彩妆香氛", @"全部", @"西安美食", @"彩妆香氛", @"全部", @"美妆运动", @"彩妆香氛", @"全部", @"美妆配饰", @"彩妆香氛"];
}
#pragma mark === 创建CollectionView ===
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    _categoryCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(TableViewWidth, 108, WIDTH - TableViewWidth, HEIGHT - 64 - 94) collectionViewLayout:layout];
    _categoryCollectionView.backgroundColor = [UIColor clearColor];
    _categoryCollectionView.delegate = self;
    _categoryCollectionView.dataSource = self;
    _categoryCollectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_categoryCollectionView];
    
    [_categoryCollectionView registerNib:[UINib nibWithNibName:@"CategoriesCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CategoriesCollectionViewCellID];
}

#pragma mark === TableViewDelegate ===
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _categoryTableArray.count;
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellID];
    cell.textLabel.text = _categoryTableArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:17];
    cell.textLabel.highlightedTextColor = APPCOLOR;
    cell.backgroundColor = BackgroundCOLOR;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TableViewWidth, TableViewCellHeight)];
    view.backgroundColor = [UIColor whiteColor];
    UIView *stick = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, TableViewCellHeight)];
    stick.backgroundColor = APPCOLOR;
    [view addSubview:stick];
    cell.selectedBackgroundView = view;

    return cell;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TableViewCellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.searchBar resignFirstResponder];
    NSLog(@"%@", _categoryTableArray[indexPath.row]);
    //将TableView被选中行滑动到最中间
    [_categoryTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    //将CollectionView置顶并显示tableView相对应的cell的内容
    [_categoryCollectionView reloadData];
    [_categoryCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionTop];
}

#pragma mark === CollectionViewDelegate ===
//item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _categoryCollectionArray.count;
}
//Reusable
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CategoriesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CategoriesCollectionViewCellID forIndexPath:indexPath];
    cell.title.text = _categoryCollectionArray[indexPath.item];
    cell.imageView.backgroundColor = [UIColor colorWithRed:VALUE green:VALUE blue:VALUE alpha:1];
    return cell;
}
//距四周边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((WIDTH - TableViewWidth) / 3, (WIDTH - TableViewWidth) / 3 + 35);
}
//被选择时
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.searchBar resignFirstResponder];
    NSLog(@"%@", _categoryCollectionArray[indexPath.item]);
    CategoryDetailViewController *cate = [[CategoryDetailViewController alloc] init];
    [self.navigationController pushViewController:cate animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.searchBar resignFirstResponder];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}

@end